package myJs.expH2

import myJs.Implicits._
import myJs.api.Api
import myJs.cps.BootstrapTableCp
import myJs.cps.BootstrapTableCp.Column
import myJs.myPkg.{layer, parse}
import myJs.tool.Pojo._
import myJs.tool.Tool
import shared.Pojo.CellData
import slinky.core.FunctionalComponent
import slinky.core.annotations.react
import slinky.core.facade.Hooks._
import slinky.web.html._
import upickle.default._

import scala.collection.immutable.SeqMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js.Dynamic.{global => g}

/**
 * Created by yz on 3/11/2020
 */
@react object TableCp {

  type Props = Unit

  val component = FunctionalComponent[Props] { props =>

    val (datas, setDatas) = useState(List.empty[Map[String,CellData]])
    val (columns, setColumns) = useState(List.empty[Column])

    object FTool {

      def refreshColumns = () => {
        val valueHeaders =
          List("kernel1.1", "kernel1.2", "kernel1.3", "kernel2", "kernel3.1", "kernel3.2", "kernel3.3", "kernel4.1", "kernel4.2", "kernel4.3.", "kernel5.1", "kernel5.2", "kernel5.3", "shell1.1", "shell1.2", "shell1.3", "shell2.1", "shell2.2", "shell2.3", "shell3.1", "shell3.2", "shell3.3", "shell4.1", "shell4.2", "shell4.3", "shell5.1", "shell5.2", "shell5.3")
        val map = SeqMap(
          "geneId" -> "ID",
        ) ++ valueHeaders.optionMap
        val basicColumns = map.map { case (k, v) =>
          val searchKind = k match {
            case x if x != "geneId" => DoubleSearch
            case _ => TextSearch
          }
          val title = v match {
            case _ => v
          }
          val operable = k match {
            case _ => true
          }
          val fmt = FTool.tbFmt(k)
          val comVF = FTool.comVFmt(v)
          Column(field = k, title = title, operable = operable, searchKind = searchKind, formatter = fmt,
            comVF = comVF)
        }.toList
        setColumns(basicColumns)
      }

      def ajaxRequest = (limit: Int, offset: Int,
                         searchMap: SeqMap[String, SearchData], sortDataOp: Option[SortData], allSearch: String) => {
        val options = Tool.layerOptions.copy(shade = List("0.1", "#fff"))
        val index = layer.load(0, options)
        val trueSortObj = if (sortDataOp.isEmpty) {
          ujson.Obj()
        } else {
          val sortData = sortDataOp.get
          ujson.Obj("sort" -> ujson.Obj(
            "field" -> sortData.field,
            "kind" -> sortData.kind,
            "order" -> sortData.order,
          ))
        }
        val searchs = searchMap.map { case (k, searchData) =>
          searchData match {
            case TextSearchData(searchType, text) =>
              ujson.Obj(
                "name" -> k,
                "kind" -> "text",
                "searchType" -> searchType,
                "value" -> text,
              )
            case NumericSearchData(min, max, kind) =>
              ujson.Obj(
                "name" -> k,
                "kind" -> kind,
                "min" -> Tool.dbFmt(min),
                "max" -> Tool.dbFmt(max),
              )
            case CheckboxSearchData(values) =>
              ujson.Obj(
                "name" -> k,
                "kind" -> "checkbox",
                "value" -> values,
              )
          }
        }.toList
        val trueSearchObj = ujson.Obj("search" -> searchs)
        val obj = ujson.Obj(
          "limit" -> limit,
          "offset" -> offset,
          "allSearch" -> allSearch
        ) ++ trueSortObj ++ trueSearchObj
        val dataStr = write(obj)
        Api.expH2.allByServer(dataStr).map { obj =>
          layer.close(index)
          obj
        }

      }


      def tbFmt(fileTitle: String) = (v: CellData, row: Map[String,CellData]) => {
        fileTitle match {
          case "geneId" =>
            val geneId=v.value.str
            val url = g.jsRoutes.controllers.Basic344Controller.detailInfo().url
            val hf = s"${url}?geneId=${geneId}&h2=true"
            a(
              target := "_blank",
              href := hf
            )(geneId)
          case _ =>
            span(v.value.myStr)()
        }
      }

      def comVFmt(fileTitle: String) = (v: CellData, row: Map[String,CellData]) => {
        fileTitle match {
          case _ => v.toString
        }
      }

    }

    useEffect(() => {
      FTool.refreshColumns()
    }, List()
    )

    div(
      BootstrapTableCp(columns = columns, datas = datas, pageList = List(10, 25, 50), pageSize = 10,
        selectColumnEnabled = true, defaultShowColumnN = 5, ajax = FTool.ajaxRequest, sidePagination = "server",
        search = true
      ),
    )

  }

}



